package com.xhsj.user.hr.userInfo.service;

import com.xhsj.user.hr.process.dto.PageDto;
import com.xhsj.user.security.entity.LoginUser;
import com.xhsj.user.hr.system.mapper.TbGlobalRoleMapper;
import com.xhsj.user.hr.userInfo.dto.AllContractListDto;
import com.xhsj.user.hr.userInfo.entity.TbGlobalUserContract;
import com.xhsj.user.hr.userInfo.entity.TbGlobalUserFile;
import com.xhsj.user.hr.userInfo.mapper.TbGlobalUserContractMapper;
import com.xhsj.user.hr.userInfo.mapper.TbGlobalUserFileMapper;
import com.xhsj.user.hr.userInfo.query.ContractListQuery;
import com.xhsj.user.hr.userInfo.service.TbGlobalUserContractService;
import com.xhsj.user.hr.base.BaseService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xhsj.user.utils.UserUtils;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.io.Serializable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import lombok.extern.slf4j.Slf4j;
/**
 * <p>
 * 用户-合同信息表 服务实现类
 * </p>
 *
 * @author liuqinghua
 * @since Sep 8, 2020 10:01:11 AM
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
//@CacheConfig(cacheNames = "TbGlobalUserContract")
public class TbGlobalUserContractService extends BaseService<TbGlobalUserContractMapper, TbGlobalUserContract>{
    @Autowired
    private TbGlobalUserContractMapper dao;

    @Autowired
    private TbGlobalRoleMapper tbGlobalRoleMapper;

    @Autowired
    private TbGlobalUserFileMapper tbGlobalUserFileMapper;
    @Autowired
    private TbGlobalUserFileService tbGlobalUserFileService;

	/**
	 * 保存
	 * @author liuqinghua
	 * @date Sep 8, 2020 10:01:11 AM
	 */
    @Override
    //@CacheEvict(cacheNames="TbGlobalUserContract", allEntries=true)
    public boolean save(TbGlobalUserContract entity) {
        return super.save(entity);
    }

	/**
	 * 通过id获取数据
	 * @author liuqinghua
	 * @date Sep 8, 2020 10:01:11 AM
	 */
    @Override
    //@Cacheable(key = "#id",sync = true)
    public TbGlobalUserContract getById(Serializable id) {
        return super.getById(id);
    }

	/**
	 * 通过id删除数据
	 * @author liuqinghua
	 * @date Sep 8, 2020 10:01:11 AM
	 */
    @Override
    //@CacheEvict(cacheNames="TbGlobalUserContract", allEntries=true)
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

	/**
	 * 批量删除数据
	 * @author liuqinghua
	 * @date Sep 8, 2020 10:01:11 AM
	 */
    //@CacheEvict(cacheNames="SysDict", allEntries=true)
    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        return super.removeByIds(idList);
    }

	/**
	 * 分页查询数据
	 * @author liuqinghua
	 * @date Sep 8, 2020 10:01:11 AM
	 */
    //@Cacheable(key = "#data",sync = true)
    public IPage<TbGlobalUserContract> selectPage(Map<String,Object> data) {
        int currentPage=1;
        if (!Objects.isNull(data.get("page"))) {
            currentPage=(Integer) data.get("page");
        }
        int pageSize=20;
        if (!Objects.isNull(data.get("size"))) {
            pageSize=(Integer) data.get("size");
        }

        TbGlobalUserContract entity = JSON.parseObject(JSON.toJSONString(data), TbGlobalUserContract.class);
        Page<TbGlobalUserContract> page = new Page(currentPage,pageSize);
        QueryWrapper<TbGlobalUserContract> wrapper = new QueryWrapper(entity);
        return super.selectPage(page, wrapper);
    }

	/**
	 * 查询所有数据
	 * @author liuqinghua
	 * @date Sep 8, 2020 10:01:11 AM
	 */
    @Override
    //@Cacheable(value = "TbGlobalUserContract",sync = true)
    public List<TbGlobalUserContract> selectAll() {
        return super.selectAll();
    }


    /**
     * 所有合同列表
     * @author liuqinghua
     * @date Sep 8, 2020 10:01:11 AM
     */
    public IPage<ContractListQuery> getAllContractList(AllContractListDto data) {
        Page<ContractListQuery> page = new Page(data.getPage(),data.getSize());
        return  dao.getAllContractList(page,data);
    }

    /**
     * 我的合同列表
     * @author liuqinghua
     * @date Sep 8, 2020 10:01:11 AM
     */
    public IPage<ContractListQuery> getMyContractList(AllContractListDto data) {
        LoginUser loginAppUser = UserUtils.getLoginAppUser();
        Page<ContractListQuery> page = new Page(data.getPage(),data.getSize());
        return dao.getMyContractList(page,loginAppUser.getUserId(),data);
    }

    /**
     * 我的合同列表
     * @author liuqinghua
     * @date Sep 8, 2020 10:01:11 AM
     */
    public List<ContractListQuery> getContractList(Long userId) {
        return dao.getContractList(userId);
    }

    /**
     * 获取最大批次
     * @author liuqinghua
     * @date Sep 8, 2020 10:01:11 AM
     */
    public int getMaxContractBatch(Long userId) {
        return  dao.getMaxContractBatch(userId);
    }

    /**
     * 保存/修改合同
     * @author liuqinghua
     * @date Sep 8, 2020 10:01:11 AM
     */
    public void saveContract(TbGlobalUserContract data) throws Exception {
        // 修改用户其他得合同作废
        TbGlobalUserContract tbGlobalUserContract = new TbGlobalUserContract();
        tbGlobalUserContract.setStatus(1);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",data.getUserId());
         super.update(tbGlobalUserContract,queryWrapper);

        if (!super.save(data)) {
            throw new Exception("保存/修改合同异常");
        }
        // 修改用户其他文件中合同
        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("user_id",data.getUserId());
        TbGlobalUserFile tbGlobalUserFile = new TbGlobalUserFile();
        tbGlobalUserFile.setDelUrl(data.getDelUrl());
        tbGlobalUserFile.setFileName(data.getFileName());
        tbGlobalUserFile.setFileUrl(data.getFileUrl());
        int update = tbGlobalUserFileMapper.update(tbGlobalUserFile, queryWrapper1);
        if (update == 0) {
            tbGlobalUserFile.setFileType(3);
            if (!tbGlobalUserFileService.save(tbGlobalUserFile)) {
                throw new Exception("保存/修改合同异常");
            }
        }

    }
}
